<template>
  <div>
    <el-dialog
      :visible.sync="isVisible"
      title="更新角色"
      center
      width="500px"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <el-form
        size="mini"
        status-icon
        label-width="50px"
        :rules="formRules"
        ref="form"
        v-if="rowData"
        :model="rowData"
      >
        <el-form-item label="角色" prop="name">
          <el-input v-model="rowData.name" autocomplete="off" placeholder="角色" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="rowData.description" autocomplete="off" placeholder="备注" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="rowData.status" placeholder="请选择状态" style="display:block;">
            <el-option label="启用" :value="0" />
            <el-option label="停用" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isVisible = false">取 消</el-button>
        <el-button
          type="primary"
          size="mini"
          :loading="loading"
          @click="updateHandle('form')"
        >
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'RoleUpdate',
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      rowData: {
        type: Object,
        default: null
      }
    },
    computed: {
      isVisible: {
        get() {
          return this.visible
        },
        set(val) {
          this.$emit('update:visible', val)
        }
      }
    },
    data() {
      return {
        loading: false,
        formRules: {
          name: [
            { required: true, message: '请输入角色', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '请输入备注', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      updateHandle(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // params
            const params = new URLSearchParams()
            params.append('id', this.rowData.id)
            params.append('name', this.rowData.name)
            params.append('description', this.rowData.description)
            params.append('status', this.rowData.status)
            // ajax
            this.loading = true
            this.AjaxUtils.put('/role/updateRole', params, true).then((response) => {
              this.loading = false
              if (response.code === 0) {
                this.isVisible = false
                this.$emit('reloadTable')
              }
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
